package Project_etow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;

import Admin.AdminControl;
import Client.ClientControl;
import GroupClass.AccountClass;
import GroupClass.ConnectRMI;

public class LoginDialog extends javax.swing.JDialog {

	
        ConnectRMI connectRMI = new ConnectRMI();
	public LoginDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
   
    @SuppressWarnings("unchecked")

    private void initComponents() {

    	lbuseName = new javax.swing.JLabel();
    	lbpassWord = new javax.swing.JLabel();
        txtuserName = new javax.swing.JTextField();
        txtpassWord = new javax.swing.JPasswordField();
        btlogin = new javax.swing.JButton();
        btexit = new javax.swing.JButton();
        setLocation(150,150);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lbuseName.setText("UserName");

        lbpassWord.setText("Password");

        btlogin.setText("Login");

        btexit.setText("Exit");
        btexit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
			    System.exit(0);
			}
		});
        
        btlogin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				try {
					EtowInterface obj=null;
					Registry registry = LocateRegistry.getRegistry(connectRMI.host,connectRMI.port);
					obj=(EtowInterface)registry.lookup(connectRMI.regis);
					String a=txtuserName.getText();
					String b=txtpassWord.getText();
					List<AccountClass> x = obj.GetLogin(a, b);
					
					
					for(GroupClass.AccountClass d:x) {
						
						if(d.getAccountid().equals(""))
						{
							JOptionPane.showMessageDialog(null, "False information");
						}
						else
						{
							if(obj.CheckGroup(d.getGroupid()).equals("Admin"))
							{
								AdminControl ac = new AdminControl();
								ac.setAccountid(d.getAccountid());
								ac.setGroupname("Admin");
								ac.setVisible(true);
								setVisible(false);							
							}
							else if (obj.CheckGroup(d.getGroupid()).equals("Client"))
							{
								ClientControl ac = new ClientControl();
								ac.setAccountid(d.getAccountid());
								ac.setGroupname("Client");
								ac.setVisible(true);
								setVisible(false);																
							}
							else  {
								
							}		
						}
																										
					}					
				}catch (Exception ex) {
				ex.printStackTrace();
				}			
			}
		});
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbpassWord)
                    .addComponent(lbuseName))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btlogin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btexit))
                    .addComponent(txtuserName, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                    .addComponent(txtpassWord))
                .addContainerGap(94, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbuseName)
                    .addComponent(txtuserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbpassWord)
                    .addComponent(txtpassWord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(64, 64, 64)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btlogin)
                    .addComponent(btexit))
                .addContainerGap(83, Short.MAX_VALUE))
        );

        pack();
    }


    public static void main(String args[]) {


        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                LoginDialog dialog = new LoginDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
	
    private javax.swing.JButton btexit;
    private javax.swing.JButton btlogin;
    private javax.swing.JLabel lbuseName;
    private javax.swing.JLabel lbpassWord;
    private javax.swing.JPasswordField txtpassWord;
    private javax.swing.JTextField txtuserName;
}

